﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SwaggerSdk
{
    public class RegexHelp
    {
        public static string SniffwebFirstCode(string code, string wordsBegin, string wordsEnd)
        {
            string NewsTitle = "";
            Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
            {
                NewsTitle = match1.Groups["title"].ToString();
                break;
            }
            return NewsTitle;
        }
        public static string SniffwebLastCode(string code, string wordsBegin, string wordsEnd)
        {
            string NewsTitle = "";
            Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
            {
                NewsTitle = match1.Groups["title"].ToString();
            }
            return NewsTitle;
        }

        public static string SniffwebForCode(string code, string wordsBegin, string wordsEnd, char split = ' ')
        {
            string NewsTitle = "";
            Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
            {

                NewsTitle += match1.Groups["title"].ToString() + split;

            }
            return NewsTitle.Trim(split);
        }

        public static string SniffwebForJieShi(string code)
        {
            code = code.Replace("</p>", "pp");
            code = code.Replace("</dt>", "dt");
            code = Regex.Replace(code, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            code = Regex.Replace(code, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            code = code.Replace("pp", "\r\n");
            int len = code.IndexOf("dt");
            if (len > 0)
            {
                int lens = code.Substring(len).IndexOf("dt");
                if (lens > 0)
                    code = code.Replace("dt", "\r\n");
                else
                    code = code.Substring(len + 2);
            }
            char[] str = "\r\n".ToCharArray();
            return code.TrimEnd(str);
        }
    }
}
